import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

typedef PickCallBack = Function(List<int> indexs);

class CustomMultiPick extends StatefulWidget {
  CustomMultiPick({
    super.key,
    required this.pickList,
    required this.confirmCallBack,
    this.cancelCallBack,
    required this.indexs,
  });

  //确认回调
  final PickCallBack confirmCallBack;

  //取消回调
  final PickCallBack? cancelCallBack;

  //数据list
  final List<String> pickList;

  //索引
  List<int> indexs;

  @override
  State<StatefulWidget> createState() {
    return CustomMultiPickerState();
  }
}

class CustomMultiPickerState extends State<CustomMultiPick> {
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
            topLeft: Radius.circular(24), topRight: Radius.circular(24)),
      ),
      child: SizedBox(
        height: 600.h,
        child: Stack(
          alignment: AlignmentDirectional.center,
          children: [
            //取消文本
            Positioned(
              left: 40.w,
              top: 24.h,
              child: InkWell(
                onTap: () {
                  widget.cancelCallBack?.call(widget.indexs) ?? Get.back();
                },
                child: Text(
                  "取消",
                  style: TextStyle(fontSize: 32.sp),
                ),
              ),
            ),

            //确认文本
            Positioned(
              right: 40.w,
              top: 24.h,
              child: InkWell(
                onTap: () {
                  widget.confirmCallBack(widget.indexs);
                  Get.back();
                },
                child: Text(
                  "确认",
                  style: TextStyle(fontSize: 32.sp),
                ),
              ),
            ),

            //列表
            Positioned(
              top: 139.h,
              height: 461.h,
              child: Row(
                children: [
                  SizedBox(
                    width: 662.w,
                    child: ListView.separated(
                      itemBuilder: (context, index) {
                        return _customText(widget.pickList[index],
                            widget.indexs.contains(index), index);
                      },
                      itemCount: widget.pickList.length,
                      separatorBuilder: (context, index) => Divider(
                        height: 0.h,
                        thickness: 0,
                        indent: 0,
                        color: Colors.transparent,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _customText(String content, bool isSelected, int index) {
    return InkWell(
      onTap: () {
        setState(() {
          if (widget.indexs.contains(index)) {
            widget.indexs.remove(index);
          } else {
            widget.indexs.add(index);
          }
        });
      },
      child: Container(
        decoration: const BoxDecoration(
          border: Border(
            top: BorderSide(width: 0.0, color: Color(0xFFD0D0D0)),
            bottom: BorderSide(width: 0.0, color: Color(0xFFD0D0D0)),
          ),
        ),
        constraints: BoxConstraints.expand(
          height: 96.h,
        ),
        padding: EdgeInsets.only(left: 48.w, right: 48.w),
        alignment: Alignment.centerLeft,
        child: Row(
          children: [
            Expanded(
              child: Text(
                content,
                style: TextStyle(fontSize: 36.sp, color: Colors.black),
              ),
            ),
            Container(
              width: 40.w,
              height: 40.h,
              decoration: BoxDecoration(
                border: Border.all(color: const Color(0xFF999999), width: 2.w),
                borderRadius: BorderRadius.all(
                  Radius.circular(20.r),
                ),
              ),
              child: Visibility(
                visible: isSelected,
                child: Image(
                  width: 40.w,
                  height: 40.h,
                  fit: BoxFit.fill,
                  image: const AssetImage(
                      "assets/images/icon_bottom_sheet_confirm.png"),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
